using UnityEngine;

public static class TextureGenerator
{
    
    /// <summary>
    /// Generate a texture from a colour map
    /// </summary>
    /// <param name="colorMap"></param>
    /// <param name="width"></param>
    /// <param name="height"></param>
    /// <returns></returns>
    public static Texture2D TextureFromColourMap(Color[] colorMap, int width,int height)
    {
        Texture2D texture = new Texture2D(width, height);
        texture.filterMode = FilterMode.Point;
        texture.wrapMode = TextureWrapMode.Clamp;
        
        texture.SetPixels(colorMap);
        texture.Apply();
        return texture;
    }

    /// <summary>
    /// Generate a texture from a height map
    /// </summary>
    /// <param name="heightMap"></param>
    /// <returns></returns>
    public static Texture2D TextureFromHeightMap(HeightMap heightMap)
    {
        int width = heightMap.value.GetLength(0);
        int height = heightMap.value.GetLength(1);
        
        Color[] colorMap = new Color[width * height];

        for (int y = 0; y < height; y++)
        {
            for (int x = 0; x < width; x++)
            {
                colorMap[y * width + x] = Color.Lerp(Color.black, Color.white,Mathf.InverseLerp(heightMap.minValue, heightMap.maxValue, heightMap.value[x, y]) );
            }
        }
        return TextureFromColourMap(colorMap, width, height);
    }
}